package com.zr.eim.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.zr.eim.domain.EimDataDiagnosticInfo;
import com.zr.eim.domain.EimDataPerson;
import com.zr.eim.service.IEimDataDiagnosticInfoService;
import com.zr.eim.service.IEimDataPersonService;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import com.zr.eim.mapper.EimDataNucleicAcidCheckMapper;
import com.zr.eim.domain.EimDataNucleicAcidCheck;
import com.zr.eim.service.IEimDataNucleicAcidCheckService;

import javax.annotation.Resource;

/**
 * 核酸检查任务Service业务层处理
 * 
 * @author peihang
 * @date 2022-10-21
 */
@Service
public class EimDataNucleicAcidCheckServiceImpl implements IEimDataNucleicAcidCheckService 
{


    @Resource
    private IEimDataPersonService personService;
    @Resource
    private IEimDataDiagnosticInfoService diagnosticInfoService;
    @Resource
    private EimDataNucleicAcidCheckMapper eimDataNucleicAcidCheckMapper;

    @Resource
    private KafkaTemplate kafkaTemplate;



    /**
     * 查询核酸检查任务
     * 
     * @param id 核酸检查任务主键
     * @return 核酸检查任务
     */
    @Override
    public EimDataNucleicAcidCheck selectEimDataNucleicAcidCheckById(Long id)
    {
        return eimDataNucleicAcidCheckMapper.selectEimDataNucleicAcidCheckById(id);
    }

    /**
     * 查询核酸检查任务列表
     * 
     * @param eimDataNucleicAcidCheck 核酸检查任务
     * @return 核酸检查任务
     */
    @Override
    public List<EimDataNucleicAcidCheck> selectEimDataNucleicAcidCheckList(EimDataNucleicAcidCheck eimDataNucleicAcidCheck)
    {
        return eimDataNucleicAcidCheckMapper.selectEimDataNucleicAcidCheckList(eimDataNucleicAcidCheck);
    }

    /**
     * 新增核酸检查任务
     * 
     * @param eimDataNucleicAcidCheck 核酸检查任务
     * @return 结果
     */
    @Override
    public int insertEimDataNucleicAcidCheck(EimDataNucleicAcidCheck eimDataNucleicAcidCheck)
    {
        return eimDataNucleicAcidCheckMapper.insertEimDataNucleicAcidCheck(eimDataNucleicAcidCheck);
    }

    /**
     * 修改核酸检查任务
     * 
     * @param eimDataNucleicAcidCheck 核酸检查任务
     * @return 结果
     */
    @Override
    public int updateEimDataNucleicAcidCheck(EimDataNucleicAcidCheck eimDataNucleicAcidCheck)
    {
        return eimDataNucleicAcidCheckMapper.updateEimDataNucleicAcidCheck(eimDataNucleicAcidCheck);
    }

    /**
     * 批量删除核酸检查任务
     * 
     * @param ids 需要删除的核酸检查任务主键
     * @return 结果
     */
    @Override
    public int deleteEimDataNucleicAcidCheckByIds(Long[] ids)
    {
        return eimDataNucleicAcidCheckMapper.deleteEimDataNucleicAcidCheckByIds(ids);
    }

    /**
     * 删除核酸检查任务信息
     * 
     * @param id 核酸检查任务主键
     * @return 结果
     */
    @Override
    public int deleteEimDataNucleicAcidCheckById(Long id)
    {
        return eimDataNucleicAcidCheckMapper.deleteEimDataNucleicAcidCheckById(id);
    }

    @Override
    public void addNucleicAcidCheck(EimDataNucleicAcidCheck info) {
        EimDataPerson p = new EimDataPerson();
        p.setPersonId(info.getPersonId());
        p.setLastCheckId(info.getId());
        if(info.getResult() == 1){

            EimDataDiagnosticInfo diagnosticInfo = new EimDataDiagnosticInfo();
            p.setIfDiagnosis(1L);
            diagnosticInfo.setCheckId(info.getId());
            diagnosticInfo.setPersonId(info.getPersonId());
            diagnosticInfo.setCreateTime(new Date());
            diagnosticInfoService.insertEimDataDiagnosticInfo(diagnosticInfo);
            kafkaTemplate.send("DiagnosticInfo", JSON.toJSONString(diagnosticInfo));
        }else{
            p.setIfDiagnosis(0L);
        }
        System.out.println(p);
        personService.updateEimDataPerson(p);
    }

    /**
     * 查询当日核酸检测数
     * 需求模块：数据大屏
     */
    @Override
    public int getCurrentNum(){
        return eimDataNucleicAcidCheckMapper.getCurrentNum();
    }
}
